WEBVTT

00:00.810 --> 00:02.980
Hello and welcome back to our case study.

00:03.000 --> 00:10.170
So in this case study we want to apply our skills and a bit larger case actually and we will apply random

00:10.170 --> 00:15.890
number generation back to arrest operations and the so-called method chaining.

00:15.930 --> 00:21.780
And also we want to compare the NUM pain and the pace and standard library in terms of performance length

00:21.810 --> 00:23.420
of code readability.

00:23.940 --> 00:29.020
Yeah and actually how intuitive the respective code might be for others.

00:29.050 --> 00:32.490
So but now let's start and let's assume the following situation.

00:32.490 --> 00:39.660
So we want to create 100 random into just between 1 and 10 and then even count how many times we get

00:39.660 --> 00:41.100
the integer one.

00:41.130 --> 00:45.000
So on average I would expect that ten times out of 100.

00:45.030 --> 00:51.150
And if you do this only one time the simulation the result can be quite different to what we expect

00:51.530 --> 00:58.800
and to check if our expectation that we get 10 times the integer one we have to simulate this many times

00:58.800 --> 01:06.080
so let's say ten thousand times to get a statistically reliable result and then be similar 10000 times

01:06.090 --> 01:12.480
and then we calculate the mean of all the outcomes are all simulations and then we should be very close

01:12.480 --> 01:16.460
to to 10 on average at least I hope actually.

01:16.500 --> 01:22.980
All right so let's start and we import an umpire and this is already the whole number code.

01:22.980 --> 01:25.670
But I would like to derive the code.

01:25.800 --> 01:34.710
So first of all we want to create 100 random integers in the range one till eleven.

01:34.710 --> 01:40.190
So we get here 100 integers and then we check which of them are equal to 1.

01:40.200 --> 01:47.430
So then we should get a ending array with the boolean objects so you can see false through false false

01:48.540 --> 01:54.300
and then we want to calculate how many times you have to be in the job on and therefore we just sum

01:54.300 --> 01:55.560
up our array.

01:55.590 --> 02:02.600
So let's remember that a true stands for the value of one and a false sense for the value zeros.

02:02.610 --> 02:11.780
So if you sum up the boolean array we should get to the number of occurrences of the enter to 1 and

02:11.780 --> 02:13.540
here we have 12.

02:13.580 --> 02:17.240
So our expectation was 10 and here only with one simulation.

02:17.310 --> 02:19.400
Yet because it's random we got twelve.

02:19.460 --> 02:28.130
And now let's repeat this process ten thousand times so we create 10000 times a hundred random integers

02:28.550 --> 02:38.180
so in total of 1 million integers and then we reshape this 1 million integers to a matrix of 10000 rows

02:38.240 --> 02:43.850
and 100 columns then for each integer we check whether it's equal to 1 or not.

02:43.850 --> 02:50.960
And then we have to sum over all rows how many troops or how many times we have the occurrence of the

02:50.960 --> 02:51.620
integer 1.

02:51.650 --> 02:55.190
So we have to take Xs so equals 1.

02:55.190 --> 02:57.070
So we sum over each row.

02:57.080 --> 03:04.550
We have 10000 simulations now and if we execute this year then we can see now we get 10000 results so

03:04.550 --> 03:07.590
we simulated this process 10000 times.

03:07.880 --> 03:13.880
And in our first simulation we got 12 times the Indians are 1 9 and the second one nine times.

03:13.900 --> 03:17.740
And now we also get six times four times so this match.

03:17.750 --> 03:20.590
Uh yeah statistical variability or actually.

03:21.350 --> 03:27.530
And yeah if you want to check whether our expectation is true that we on average expect 10 occurrences

03:27.530 --> 03:35.120
of the integer one we have to take the mean of all the results and then I can see we are very close

03:35.120 --> 03:35.690
to 10.

03:36.830 --> 03:42.770
So now we created the many random numbers we use to vector us operations and also the so-called method

03:42.770 --> 03:45.980
chaining so we changed one method after another.

03:45.980 --> 03:49.620
So we started to create a random integer then we reshape that.

03:49.850 --> 03:55.770
Then we took the sum of our troops for each row and then we took the mean of all results.

03:55.790 --> 04:03.860
So this is called Method chaining and we can also measure the time that this process or this code takes

04:03.860 --> 04:05.780
on average this time.

04:05.790 --> 04:06.350
Function

04:15.900 --> 04:22.850
that takes up a time so we have one million random integers so we got here 60 milliseconds.

04:22.850 --> 04:28.970
And now what we want to do now we actually want to simulate the same process with the pace and standard

04:28.970 --> 04:31.450
library and therefore we have to impart the random module.

04:32.660 --> 04:38.750
And yes actually the code that does the same job as 0 0 an umpire code with Python standard library

04:38.930 --> 04:39.620
instrument.

04:39.630 --> 04:42.610
So let's start here with the inner for loop.

04:42.740 --> 04:47.920
So a hundred times we are creating a random integer in the range between 1 and 10.

04:47.930 --> 04:50.630
And then we check for each created random integer.

04:50.660 --> 04:55.340
If it's equal to 1 and if it's 1 We append a true to our list.

04:55.360 --> 05:00.080
Edit And if it's not equal to 1 We append files to our list.

05:00.590 --> 05:07.760
And then we calculate the sum of all of our truisms and false in our list and append this to a resides

05:07.760 --> 05:09.290
list and indeed resides.

05:09.290 --> 05:13.940
Lists are all reserved for in total ten thousand simulations.

05:13.940 --> 05:17.440
I put this whole code here into a function called simulation.

05:17.450 --> 05:20.840
So that makes it easier later yet to measure the time actually.

05:21.140 --> 05:24.500
So there's no deeper reason for it.

05:24.500 --> 05:29.750
So they can execute the Sally and then we can run our function and simulate that space in standard library

05:32.050 --> 05:34.330
and I can see we got a nine point nine.

05:34.330 --> 05:37.430
Also here we are very close to 10.

05:37.450 --> 05:43.010
Now let's measure the time of the performance of the code and compare it to an umpire.

05:52.760 --> 05:54.580
We have here one point two seconds.

05:54.580 --> 06:01.210
So this code took us one point two seconds and an umpire did actually the same job in six in milliseconds.

06:01.210 --> 06:03.400
Some umpires 100 times faster.

06:03.460 --> 06:05.510
And that's actually a huge difference.

06:05.830 --> 06:12.160
And you can also see here we have a very condensed code so we have actually only one line of code here.

06:12.220 --> 06:18.060
And in contrast with sort of the pace the standard library we have here nested for loop and if statements.

06:18.100 --> 06:22.860
However some people say that here the readability of the code is much easier.

06:22.870 --> 06:25.750
And also it's what you'd have to understand.

06:25.750 --> 06:31.060
So at least if you have done two or three times the nested blue band some statements it might be quite

06:31.060 --> 06:36.310
easy to understand what the code is doing here in comparison the vector as campaign code and method

06:36.340 --> 06:41.170
chaining might be a bit less intuitive at least if you are not used to it.

06:41.170 --> 06:41.550
All right.

06:41.560 --> 06:46.780
So this is it for the time being and in the next session we will have a look at summary statistics so

06:47.050 --> 06:48.430
help to see there by.
